package com.futao.springbootlearn20220224.aware;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 感知容器，获取SpringContext容器
 *
 * @author futao@gmail.com
 * @date 2022/3/9
 */
@Slf4j
@Component
public class ApplicationContextAwareDemo implements ApplicationContextAware {

    public static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("这样就可以拿到容器，以供使用:{}", applicationContext);
        ApplicationContextAwareDemo.setApplicationContextLocal(applicationContext);
    }

    private static void setApplicationContextLocal(ApplicationContext applicationContext) {
        ApplicationContextAwareDemo.applicationContext = applicationContext;
    }
}
